package com.xbongbong.paas.help;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.pojo.dto.ImportFormDataDTO;
import com.xbongbong.paas.pojo.dto.ImportMessageFormDataDTO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisTimeConstant;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.rabbitmq.producer.ImportProducer;
import com.xbongbong.saas.enums.CompanyConfigEnum;
import com.xbongbong.sys.domain.entity.CompanyConfigEntity;
import com.xbongbong.sys.model.CompanyConfigModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 导入像消费者发送消息 helper
 * @author 魏荣杰
 *
 */
@Component
@Slf4j
public class ImportSendHelper {

    @Resource
    private CompanyConfigModel companyConfigModel;
    @Resource
    private ImportProducer importProducer;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     * Description: 发送导入消费消息，判断是否公司走大数据处理信道
     * @param importMessageFormDataDTO
     * @return void
     * @throws
     * @author 魏荣杰
     * @date 2022/9/14 14:33
     * @since
     */
    public void sendMessage(ImportMessageFormDataDTO importMessageFormDataDTO) {

        try {
            ImportFormDataDTO importFormDataDTO = importMessageFormDataDTO.getImportFormDataDTO();
            String corpid = importFormDataDTO.getCorpid();
            if (StringUtil.isEmpty(corpid)) {
                return;
            }

            if (checkIsImportLargeNum(corpid))  {
                importProducer.sendLargeNumMessage(importMessageFormDataDTO);
            } else {
                importProducer.sendMessage(importMessageFormDataDTO);
            }
        } catch (Exception e) {
           log.error("com.xbongbong.paas.help.ImportSendHelper.sendMessage fail", e);
        }


    }

    /**
     * Description: 判断公司是否是走导入大数据队列
     * @param corpid
     * @return boolean
     * @throws
     * @author 魏荣杰
     * @date 2022/9/14 14:54
     * @since
     */
    public boolean checkIsImportLargeNum(String corpid) {
        String value = paasRedisHelper.getValue(CompanyConfigEnum.IMPORT_LARGE_NUM.getAlias(), corpid);

        if (StringUtil.isEmpty(value)) {
            CompanyConfigEntity companyConfigEntity = companyConfigModel.getByConfigAlias(CompanyConfigEnum.IMPORT_LARGE_NUM.getAlias(), corpid);
            if (Objects.nonNull(companyConfigEntity)) {
                value = companyConfigEntity.getConfigValue();

                JSONObject jsonObject = JSONObject.parseObject(value);
                String enable = jsonObject.getString("enable");
                Long expirationTime = jsonObject.getLong("expirationTime");
                if (expirationTime > DateTimeUtil.getInt()) {
                    paasRedisHelper.setValue(CompanyConfigEnum.IMPORT_LARGE_NUM.getAlias(), corpid, enable, RedisTimeConstant.TEN_MINUTES);
                    return Objects.equals(enable, CompanyConfigEnum.IMPORT_LARGE_NUM.getValue());
                } else {
                    return false;
                }
            }
        }

        return Objects.equals(value, CompanyConfigEnum.IMPORT_LARGE_NUM.getValue());
    }

}
